package com.firexxx.vp.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author jchuang
 * @Description: 配置工具类
 * @Date Created in 9:23 2018/8/27
 * @Modified By:
 */
@Slf4j
public class ConfigUtil {
    private static Properties props = new Properties();

    static {
        try {
            props.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")));
        } catch (Exception e) {
            log.error("errormsg=初始化配置文件失败", e);
        }
    }

    public static void update(Properties p) {
        props = p;
        log.info("infomsg=ConfigUtil properties update success");
    }

    public static int getInt(String key, int defaultValue) {
        String value = props.getProperty(key);
        if ("".equals(value) || value == null) {
            return defaultValue;
        }
        try {

            return Integer.parseInt(value);

        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static String getValue(String key) {
        return props.getProperty(key);
    }

    public static String getValue(String key, String defValue) {
        String ret = props.getProperty(key);
        if (ret == null || ret.isEmpty() || ret.equals("")) {
            return defValue;
        }
        return ret;
    }
}
